package cn.bgs.controller;

import cn.bgs.common.AliOss;
import cn.bgs.common.R;
import cn.bgs.pojo.ProjectType;
import cn.bgs.service.IMaintenanceService;
import cn.bgs.utils.RedisUtil;
import cn.bgs.vo.MaintenanceIntermediateVo;
import cn.bgs.vo.MaintenanceListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 维修记录表 前端控制器
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/maintenance")
public class MaintenanceController {

    @Autowired
    private AliOss aliOss;

    @Autowired
    private IMaintenanceService iMaintenanceService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询用户所有维修记录
     * @param userId
     * @return
     */
    @GetMapping("/getMaintenanceList")
    public R getMaintenanceList(Integer userId){
       List<MaintenanceListVo> list=iMaintenanceService.getMaintenanceList(userId);
       return R.ok().data("list",list);
    }

    /**
     * 查询级联维修类型和维修项目
     * @return
     */
    @GetMapping("/getMaintenceTree")
    public R getMaintenceTree(){
      List<ProjectType>  list=iMaintenanceService.getMaintenceTree();
      return R.ok().data("list",list);
    }

    /**
     * 申请维修
     * @param maintenanceIntermediateVo
     * @return
     */
    @PostMapping("/saveMaintence")
    public R saveMaintence(@RequestBody MaintenanceIntermediateVo maintenanceIntermediateVo){
        int i=iMaintenanceService.saveMaintence(maintenanceIntermediateVo);
        if (i>0){
            return R.ok().success(true);
        }else {
            return R.ok().data("error","失败");
        }
    }

    /**
     * 添加维修图片
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public String savePicture(MultipartFile file){
        String file1=aliOss.upload(file);
        return file1;
    }

    /**
     * 撤销申请
     * @return
     */
    @DeleteMapping("/deleteMaintence")
    public R deleteMaintence(Integer maintenanceId){
        int i=iMaintenanceService.deleteMaintence(maintenanceId);
        if (i <= 0) {
            return R.ok().data("errorr","失败");
        } else {
            return R.ok().code(20000);
        }
    }
}
